package com.to.aboomy.bannersample.indicator;

import com.to.aboomy.banner.Indicator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * DashPointView指示器
 *
 * @since 2021-08-05
 */
public class DashPointView extends DirectionalLayout implements Indicator {
    private Context context;
    private int pointRadius  = 10;
    private int pointSelectColor = Color.RED.getValue();
    private int pointColor = Color.GRAY.getValue();
    private int maxPointWidth = pointRadius * 6;
    private int pointWidth = pointRadius * 2;
    private int pointHeight = pointRadius * 2;
    private int spacing = pointRadius;

    public DashPointView(Context context) {
        super(context);
        this.context = context;
        setOrientation(DirectionalLayout.HORIZONTAL);
    }

    public DashPointView(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    public DashPointView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    @Override
    public void initIndicatorCount(int pagerCount) {
        setVisibility(pagerCount > 1 ? VISIBLE : INVISIBLE);
        removeAllComponents();
        DirectionalLayout.LayoutConfig lp = new DirectionalLayout.LayoutConfig(LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        lp.setMarginLeft(spacing);
        lp.setMarginRight(spacing);
        for (int i = 0; i < pagerCount; i++) {
            // 翻页指示的点
            Text pointView = new Text(getContext());
            pointView.setLayoutConfig(lp);
            ShapeElement drawable = new ShapeElement();
            drawable.setCornerRadiiArray(new float[]{pointRadius, pointRadius, pointRadius, pointRadius, pointRadius, pointRadius, pointRadius, pointRadius});
            if (0 == i) {
                pointView.setWidth(maxPointWidth);
                drawable.setRgbColor( RgbColor.fromArgbInt(pointSelectColor));
                pointView.setSelected(true);
            } else {
                pointView.setWidth(pointWidth);
                drawable.setRgbColor(RgbColor.fromArgbInt(pointColor));
                pointView.setSelected(false);
            }
            pointView.setHeight(pointHeight);
            pointView.setBackground(drawable);
            addComponent(pointView);
        }
    }
    @Override
    public Component getView() {
        return this;
    }
    DependentLayout.LayoutConfig params = null;
    @Override
    public DependentLayout.LayoutConfig getParams() {
        if (params == null) {
            params = new DependentLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, 50);
            params.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_BOTTOM);
            params.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT);
            params.addRule(DependentLayout.LayoutConfig.BELOW);
            params.setMarginBottom(AttrHelper.vp2px(10, context));
        }
        return params;
    }

    @Override
    public void setAutoStau(Boolean isAuto) {

    }

    @Override
    public void onPageSliding(int i, float v, int i1) {
        System.out.println("onPageSliding");
    }

    @Override
    public void onPageSlideStateChanged(int i) {
        System.out.println("onPageSlideStateChanged");
    }

    @Override
    public void onPageChosen(int position) {
        selectPoint(position);
    }


    public void selectPoint(int position) {
        final int count = getChildCount();
        if (position >= 0 && position < count) {
            Text pointView;
            for (int i = 0; i < count; i++) {
                pointView = (Text) getComponentAt(i);
                if (position == i) {
                    selectPoint(pointView);
                } else {
                    unSelectPoint(pointView);
                }
            }
        }
    }

    private void selectPoint(final Text v) {
        if (v != null && !v.isSelected()) {
            final float temp = pointRadius * 4f / 300f;
            final ShapeElement drawable = (ShapeElement) v.getBackgroundElement();
            drawable.setRgbColor(RgbColor.fromArgbInt(pointSelectColor));
            AnimatorValue animatorValue = new AnimatorValue();
            animatorValue.setDuration(300);
            animatorValue.setValueUpdateListener((animatorValue1, v1) -> {
                //等于当前时间减去动画开始的时间。
                float width = pointWidth + (v1*300 * temp);
                if (v1 >= 1) {
                    width = maxPointWidth;
                }
                v.setWidth((int) width);
            });
            animatorValue.start();
            v.setSelected(true);
        }
    }

    private void unSelectPoint(final Text v) {
        if (v != null && v.isSelected()) {
            final float temp = pointRadius * 4f / 300f;
            final ShapeElement drawable = (ShapeElement) v.getBackgroundElement();
            drawable.setRgbColor(RgbColor.fromArgbInt(pointColor));
            AnimatorValue animatorValue = new AnimatorValue();
            animatorValue.setDuration(300);
            animatorValue.setValueUpdateListener((animatorValue1, v1) -> {
                float width = maxPointWidth - (Math.min(v1*300, 300) * temp);
                if (v1 >= 1) {
                    width = pointWidth;
                }
                v.setWidth((int) width);
            });
            animatorValue.start();
            v.setSelected(false);
        }
    }
}
